package services;

import dao.TestCaseJpaController;
import entities.TestCase;
import entities.Problem;
import vo.VOTestCase;

public class TestCaseService {
   private static TestCaseJpaController tc;
   private static ProblemService ps;

   private TestCaseJpaController getJPAController()
   {
       if(tc == null)
           tc = new TestCaseJpaController();
       return tc;
   }

   private ProblemService getProblemService()
   {
       if(ps == null)
           ps = new ProblemService();
       return ps;
   }

   public boolean saveTestCase(VOTestCase vo)
   {
       boolean ret = true;
       tc = getJPAController();
       ps = getProblemService();
       Problem p = null;
       TestCase t = new TestCase(0,vo.getInput(),vo.getOutput());
       try
       {
           p = ps.getProblem(vo.getProblemName());
           t.setProblem(p);
           tc.create(t);
       }
       catch (Exception e)
       {
           System.out.println("Exception at TestCaseService.saveTestCase: " + e.getMessage());
           ret = false;
       }
       return ret;
   }

   public TestCase getTestCase(int id)
   {
       tc = getJPAController();
       return tc.findTestCase(id);
   }

   public boolean editTestCase(TestCase t)
   {
       tc = getJPAController();
       try
       {
           tc.edit(t);
           return true;
       } catch (Exception e)
       {
           return false;
       }
   }

   public boolean deleteTestCase(TestCase t)
   {
       tc = getJPAController();
       try
       {
           tc.destroy(t.getIdTestCase());
           return true;
       }
       catch (Exception e)
       {
           return false;
       }
   }
}